﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Yipusoft.Account.BLL;

public partial class KeyUnit_KeyUnitList2 : System.Web.UI.Page
{
    string temptitle = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindChar();
            BindTable();
            BindKeyUnitType();
        }
    }
    private void BindTable()
    {
        string tempstr = "";

        if (Request["DepartmentID"] == null || Request["Flag"] == null)
            return;
        //支队
        if (Request["Flag"] == "1")
        {
            tempstr = "GetPowerTotal23";
            temptitle = "防火处";
        }
        //大队
        if (Request["Flag"] == "2")
        {
            tempstr = "GetPowerTotal22";
            temptitle = "各大队";
        }
        //派出所
        if (Request["Flag"] == "3")
        {
            tempstr = "GetPowerTotal24";
            temptitle = "各派出所";
        }

        if (tempstr == "")
            return;

        lblTitle.Text = Department.GetDepartment(Request["DepartmentID"].ToString()).FullName + temptitle + "完成情况";
        string departmentID = Request["DepartmentID"];
        DataTable dtb = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand(tempstr, con);
        cmd.Parameters.Add("@DepartmentID", SqlDbType.NVarChar, 50).Value = departmentID;
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        dap.Fill(dtb);

        DataTable dt = dtb.Copy();

        //添加汇总行
        DataRow dr = dtb.NewRow();
        dr[0] = "合计";

        for (int i = 0; i < dtb.Rows.Count; i++)
        {
            for (int j = 0; j < dtb.Rows[i].ItemArray.Count(); j++)
            {
                if (i == 0 || j == 0)
                {
                    string temp = dtb.Rows[i][j].ToString();
                    string[] str = temp.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    if (str.Length > 1)
                    {
                        dtb.Rows[i][j] = str[0].ToString();
                    }
                }
                if (j != 0)
                {
                    if (dr[j].ToString() == "")
                        dr[j] = 0;
                    dr[j] = Convert.ToInt32(dr[j]) + Convert.ToInt32(dtb.Rows[i][j].ToString());
                }
            }
        }

        dtb.Rows.Add(dr);
        
        this.GridView1.DataSource = dtb;
        this.GridView1.DataBind();
        
        for (int i = 0; i < GridView1.Rows.Count-1; i++)
        {
            for (int j = 1; j < GridView1.Rows[i].Cells.Count; j++)
            {
                string temp = GridView1.Rows[i].Cells[j].Text;
                string url = dt.Rows[i][0].ToString();
                string departmentID1 = url.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[1].ToString();
                string[] type = GridView1.HeaderRow.Cells[j].Text.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                if (type.Length > 1)
                {
                    GridView1.Rows[i].Cells[j].Text = "<a href='ListView.aspx?DepartmentID=" + departmentID1 + "&TypeID=" + type[1].ToString() + "&Flag=" + (int.Parse(Request["Flag"].ToString()) + 1) + "'>" + temp + "</a>";
                }
            }
        }

        for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
        {
            string[] temp = GridView1.HeaderRow.Cells[i].Text.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (temp.Length > 1)
                GridView1.HeaderRow.Cells[i].Text = temp[0].ToString();
        }

    }
    private void BindKeyUnitType()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("Select * From KeyUnitType  order by cast(KeyUnitTypeID as int)", con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataTable dtb = new DataTable();
        dap.Fill(dtb);
        this.Repeater1.DataSource = dtb;
        this.Repeater1.DataBind();
    }
    private void BindChar()
    {

        string temp = "";
        string titlep = "";
        if (Request["DepartmentID"] == null || Request["Flag"] == null)
            return;
        //支队
        if (Request["Flag"] == "1")
        {
            temp = "KeyUnit4PowerDadui1";
            titlep = "防火处";
        }
        //大队
        if (Request["Flag"] == "2")
        {
            temp = "KeyUnit4PowerDadui";
            titlep = "各大队";
        }
        //派出所
        if (Request["Flag"] == "3")
        {
            temp = "KeyUnit4PowerDadui2";
            titlep = "各派出所";
        }

        if (temp == "")
            return;


        string departmentID = Request["DepartmentID"];
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand(temp, con);
        cmd.Parameters.Add("@DepartmentID", SqlDbType.NVarChar, 50).Value = departmentID;
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataTable dtb = new DataTable();
        dap.Fill(dtb);
        DataView vw = dtb.DefaultView;
        UserChart.Titles["Title"].Text = Department.GetDepartment(departmentID).FullName + titlep + "完成情况统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        UserChart.Series["录入完成率"].Points.DataBindXY(vw, "ShortName", vw, "Score");
        //for (int i = 0; i < vw.Count; i++)
        //{
        //    UserChart.Series["录入完成率"].Points[i].Href = "/KeyUnit/KeyUnitList.aspx?DepartmentID=" + vw[i][1].ToString();

        //}
    }
}
